package com.ithuangyl.common.base;

import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ithuangyl.common.R;
import com.ithuangyl.common.loader.LatteLoader;
import com.ithuangyl.common.mvp.base.BaseMvpFragment;
import com.ithuangyl.common.mvp.base.BaseMvpPresenter;
import com.ithuangyl.common.mvp.base.IBaseMvpView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

/**
 * author : itHuang
 * e-mail : ithuangyl@163.com
 * date   : 2020/7/18 11:47
 */
public abstract class BaseListFragment<T, V extends IBaseMvpView, P extends BaseMvpPresenter<V>> extends BaseMvpFragment<V, P> implements OnRefreshListener, OnLoadMoreListener {
    SmartRefreshLayout vRefreshLayout;
    RecyclerView vRecyclerView;

    private boolean isVertical = true;//列表方向

    public static final int LINEAR_LAYOUT = 1;
    public static final int GRID_LAYOUT = 2;
    public static final int STAGGERED_GRID = 3;
    private int layoutType;//列表类型

    private static final int DEFAULT_SPAN_COUNT = 2;

    private int spanCount;//列表列数

    private int currentPageIndex = 1;
    private boolean isEnableLoadMore = true;//是否允许加载更多

    protected BaseQuickAdapter<T, BaseViewHolder> mAdapter = null;

    @Override
    protected Object getLayout() {
        return R.layout.fragment_base_list;
    }

    @Override
    protected void initView(View rootView) {
        vRefreshLayout = $(R.id.id_refreshLayout);
        vRecyclerView = $(R.id.id_recyclerView);

        layoutType = getLayoutType();
        isVertical = setOrientation();
        spanCount = setSpanCount();
        isEnableLoadMore = isEnableLoadMore();
        switch (layoutType) {
            case LINEAR_LAYOUT:
                LinearLayoutManager linearManager = new LinearLayoutManager(mActivity);
                linearManager.setOrientation(isVertical ? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL);
                vRecyclerView.setLayoutManager(linearManager);
                break;
            case GRID_LAYOUT:
                GridLayoutManager gridManager = new GridLayoutManager(mActivity, spanCount);
                gridManager.setOrientation(isVertical ? GridLayoutManager.VERTICAL : GridLayoutManager.HORIZONTAL);
                vRecyclerView.setLayoutManager(gridManager);
                break;
            case STAGGERED_GRID:
                StaggeredGridLayoutManager staggeredManager = new StaggeredGridLayoutManager(spanCount, isVertical ? StaggeredGridLayoutManager.VERTICAL : StaggeredGridLayoutManager.HORIZONTAL);
                vRecyclerView.setLayoutManager(staggeredManager);
                break;
            default:
                throw new RuntimeException("Wrong layout type, it can only be 1 or 2 or 3!");
        }

        mAdapter = setAdapter();

        if (mAdapter != null) {
            vRecyclerView.setAdapter(mAdapter);
        }
    }

    @Override
    protected void initViewed() {
        vRefreshLayout.setEnableFooterFollowWhenLoadFinished(true);
        vRefreshLayout.setOnRefreshListener(this);
        vRefreshLayout.setOnLoadMoreListener(this);
        vRefreshLayout.setEnableLoadMore(isEnableLoadMore);
    }

    protected boolean setOrientation() {
        return isVertical;
    }

    protected int getLayoutType() {
        return LINEAR_LAYOUT;
    }

    protected int setSpanCount() {
        return DEFAULT_SPAN_COUNT;
    }

    protected abstract BaseQuickAdapter<T, BaseViewHolder> setAdapter();

    /**
     * 手动调用该函数，设置列表页面是否允许加载跟多
     *
     * @param isEnable
     */
    protected void setEnableLoadMore(boolean isEnable) {
        this.isEnableLoadMore = isEnable;
        vRefreshLayout.setEnableLoadMore(isEnable);
    }

    /**
     * 继承该函数，设置列表页面是否允许加载跟多，默认为允许
     *
     * @return
     */
    protected boolean isEnableLoadMore() {
        return true;
    }

    protected void requestData(int pageIndex) {

    }

    @Override
    protected void onLazyLoad() {
        currentPageIndex = 1;
        requestData(currentPageIndex);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        currentPageIndex = 1;
        //如果当前是允许加载更多的。刷新完之后就要从新设置还有更多的数据数据可加载，否则就设为没有跟多的数据了
        vRefreshLayout.setNoMoreData(!isEnableLoadMore);
        requestData(currentPageIndex);
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        currentPageIndex++;
        requestData(currentPageIndex);
    }

    /**
     * 加载结束
     */
    protected void finishRequest() {
        LatteLoader.stopLoading();
        if (null != vRefreshLayout) {
            if (vRefreshLayout.isRefreshing()) {
                vRefreshLayout.finishRefresh();
            }
            if (vRefreshLayout.isLoading()) {
                vRefreshLayout.finishLoadMore();
            }
        }
    }

    /**
     * 没有更多数据了
     */
    protected void setNoMoreData() {
        if (null != vRefreshLayout) {
            if (vRefreshLayout.isLoading()) {
                vRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                vRefreshLayout.setNoMoreData(true);
            }
        }
    }

    /**
     * 处理数据
     */
    public void handlerListData(int pageCount, List<T> list) {
        finishRequest();
        if (currentPageIndex >= pageCount) {
            setNoMoreData();
        }

        if (currentPageIndex == 1) {
            if (list == null || list.size() == 0) {
                //展示无数据的界面

            } else {
                mAdapter.setNewInstance(list);
            }
        } else {
            if (list == null || list.size() == 0) {
                setNoMoreData();
            } else {
                mAdapter.addData(list);
            }
        }

        class ViewTest implements IBaseMvpView {

        }
    }
}
